'''
Created on Feb 2, 2016

@author: JimmyMo
'''
import searchresult
import elasticsearch_da
from crypt import methods
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'

from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash

from flask_script import Manager
from flask_bootstrap import Bootstrap

app = Flask(__name__)
#app.config.from_object(__name__)
bootstrap = Bootstrap(app)
#app.config.from_envvar()

@app.route("/search", methods=['GET', 'POST'])
def search():
    app.logger.debug("enter search...")
    keywords = request.args.get('keywords')
    sr = elasticsearch_da.full_text_search_results(keywords)
    return render_template('searchresult.html', sr=sr, keywords=keywords)

@app.route("/")
def index():
    app.logger.debug("enter index...")
    return render_template('index.html',name="ICBCSou - Start from here!")

@app.route("/input")
def input():
    app.logger.debug("enter input...")
    return render_template('rawinput.html')

@app.route("/save", methods=['post'])
def save():
    app.logger.debug("ender save...")
    title = request.form['txtTitle']
    content = request.form['txtContent']
    elasticsearch_da.create_record(title, content, 'jimmymo', searchresult.RAW_INPUT, "")
    return render_template('rawinput_success.html')

@app.route("/detail")
def detail():
    app.logger.debug("enter detail...")
    id = request.args.get('id')
    a = elasticsearch_da.search_by_id(id, "icbcsou_raw")
    return render_template('rawdetail.html',sr=a)

if __name__ == "__main__":
    app.debug = True
    app.run(host="0.0.0.0")